//package com.gylang.sso.filter;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.gylang.common.jwt.JwtRole;
//import com.gylang.common.jwt.JwtUser;
//import com.gylang.common.jwt.JwtUtils;
//import com.gylang.common.utils.JsonUtils;
//import com.gylang.common.model.form.LoginForm;
//import com.gylang.sso.config.RsaKeyProperties;
//import lombok.AllArgsConstructor;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.AuthenticationServiceException;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.io.PrintWriter;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
// * @author gylang,
// * @data 2019/12/7 12:21,
// * @DESC
// */
//
//@AllArgsConstructor
//public class JwtLoginFilter extends UsernamePasswordAuthenticationFilter {
//
//    private AuthenticationManager authenticationManager;
//    private RsaKeyProperties properties;
//
//    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
//
//        try {
//            System.out.println("request.getInputStream() :\n" +  new ObjectMapper().readValue(request.getInputStream(), LoginForm.class));
//            LoginForm loginForm = new ObjectMapper().readValue(request.getInputStream(), LoginForm.class);
//            UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(loginForm.getUsername(), loginForm.getPassword());
//            return authenticationManager.authenticate(authRequest);
//        } catch (Exception e) {
//            try {
//                response.setContentType("application/json;charset=utf-8");
//                response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
//                PrintWriter out = response.getWriter();
//                Map result = new HashMap();
//                result.put("code", HttpServletResponse.SC_UNAUTHORIZED);
//                result.put("msg", "用户名密码错误");
//                out.write(new ObjectMapper().writeValueAsString(result));
//                out.flush();
//                out.close();
//            } catch (IOException e1) {
//
//            }
//
//        }
//    }
//
//    @Override
//    public void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
//
//        JwtUser jwtUser = new JwtUser();
//        jwtUser.setUsername(authResult.getName());
//        jwtUser.setRoles((List<JwtRole>) authResult.getPermission());
//        String token = JwtUtils.generateTokenExpireInMinute(jwtUser, properties.getPrivateKey(), 24 * 60);
//        response.addHeader("Authorization", "Bearer" + token);
//        try {
//            response.setContentType("application/json;charset=utf-8");
//            response.setStatus(HttpServletResponse.SC_OK);
//            PrintWriter out = response.getWriter();
//            Map result = new HashMap();
//            result.put("code", HttpServletResponse.SC_OK);
//            result.put("msg", "认证成功!");
//            out.write(new ObjectMapper().writeValueAsString(result));
//            out.flush();
//            out.close();
//        } catch (IOException e1) {
//            e1.printStackTrace();
//        }
//    }
//}
